using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace Acms.WebControls
{
    [ToolboxData("<{0}:TextBoxField runat=server></{0}:TextBoxField>")]
    public class TextBoxField : DataControlField
    {
        protected override DataControlField CreateField()
        {
            return new TextBoxField();
        }

        public override void InitializeCell(DataControlFieldCell cell, DataControlCellType cellType, DataControlRowState rowState, int rowIndex)
        {
            if (cellType == DataControlCellType.DataCell)
            {
                if ((rowState & DataControlRowState.Edit) != 0 ||
                    (rowState & DataControlRowState.Insert) != 0)
                {
                    TextBox txt = new TextBox();
                    DataControlFieldCollection fields = null;
                    if(this.Control is GridView)
                    {
                        fields = ((GridView)this.Control).Columns;
                    }
                    else if(this.Control is DetailsView)
                    {
                        fields = ((DetailsView)this.Control).Fields;
                    }
                    else
                    {
                        throw new NotImplementedException(string.Format("Control of type '{0}' not handeled please do so....", this.Control.GetType().ToString()));
                    }

                    int fieldIndex = 0;
                    if(fields != null)
                    {
                        fieldIndex = fields.IndexOf(this);
                    }

                    txt.ID = string.Concat(rowIndex, "_", fieldIndex, "_txt");
                    cell.Controls.Add(txt);
                    txt.DataBinding += new EventHandler(txt_DataBinding);
                    txt.Width = this.Width;
                    txt.MaxLength = MaxLength;

                    if (Required)
                    {
                        RequiredFieldValidator val = new RequiredFieldValidator();
                        val.ControlToValidate = txt.ID;
                        val.Display = RequiredDisplay;
                        if (!string.IsNullOrEmpty(RequiredMessage))
                        {
                            val.ErrorMessage = RequiredMessage;
                        }
                        else
                        {
                            val.ErrorMessage = "*Required";
                        }
                        if (!string.IsNullOrEmpty(RequiredValidationGroup))
                        {
                            val.ValidationGroup = RequiredValidationGroup;
                        }

                        cell.Controls.Add(val);
                    }
                }
                else
                {
                    Label lbl = new Label();
                    cell.Controls.Add(lbl);
                    lbl.DataBinding += new EventHandler(lbl_DataBinding);
                }
            }
            else if (cellType == DataControlCellType.Header && !string.IsNullOrEmpty(HeaderText))
            {
                cell.Text = HeaderText;
            }
            else if (cellType == DataControlCellType.Footer && !string.IsNullOrEmpty(FooterText))
            {
                cell.Text = FooterText;
            }
        }

        private void txt_DataBinding(object sender, EventArgs e)
        {
            TextBox txt = sender as TextBox;
            IDataItemContainer container = GetDataItemContainer(txt);
            if (txt != null && container != null)
            {
                object val = DataBinder.Eval(container.DataItem, DataField);
                if (val != null)
                {
                    txt.Text = val.ToString();
                }
            }
        }

        private void lbl_DataBinding(object sender, EventArgs e)
        {
            Label lbl = sender as Label;
            IDataItemContainer container = GetDataItemContainer(lbl);
            if (lbl != null && container != null)
            {
                object val = DataBinder.Eval(container.DataItem, DataField);
                if (val != null)
                {
                    lbl.Text = val.ToString();
                }
            }
        }

        private IDataItemContainer GetDataItemContainer(Control control)
        {
            IDataItemContainer container = null;
            Control current = control;
            while (container == null && current != null && current.NamingContainer != null)
            {
                current = current.NamingContainer;
                container = current as IDataItemContainer;
            }
            return container;
        }

        private string dataField;
        public string DataField
        {
            get { return dataField; }
            set { dataField = value; }
        }

        private Unit width;
        public Unit Width
        {
            get { return width; }
            set { width = value; }
        }

        private int maxLength;
        public int MaxLength
        {
            get { return maxLength; }
            set { maxLength = value; }
        }

        private bool required;
        public bool Required
        {
            get { return required; }
            set { required = value; }
        }

        private string requiredMessage;
        public string RequiredMessage
        {
            get { return requiredMessage; }
            set { requiredMessage = value; }
        }

        private ValidatorDisplay requiredDisplay = ValidatorDisplay.Dynamic;
        public ValidatorDisplay RequiredDisplay
        {
            get { return requiredDisplay; }
            set { requiredDisplay = value; }
        }

        private string requiredValidationGroup;
        public string RequiredValidationGroup
        {
            get { return requiredValidationGroup; }
            set { requiredValidationGroup = value; }
        }

        public override void ExtractValuesFromCell(System.Collections.Specialized.IOrderedDictionary dictionary, DataControlFieldCell cell, DataControlRowState rowState, bool includeReadOnly)
        {
            if(cell.Controls.Count > 0 && !string.IsNullOrEmpty(this.DataField))
            {
                TextBox txt = cell.Controls[0] as TextBox;
                if (txt != null)
                {
                    dictionary.Add(this.DataField, txt.Text);
                }
            }
        }
	
	
	
    }
}
